//
//  HomeViewController.m
//  livemobile
//
//  Created by iris on 2017/11/30.
//  Copyright © 2017年 iris. All rights reserved.
//

#define isIPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)

#define STATUS_BAR_HEIGHT [UIApplication sharedApplication].statusBarFrame.size.height

/** 主屏幕宽高 */
#define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT  [UIScreen mainScreen].bounds.size.height
//适配
//#define AutolayoutWidth(w) w*(SCREEN_WIDTH/375)
//#define AutolayoutHeight(h) h*([SCREEN_HEIGHT/667)

/** 通知中心 */
#define KNotificationCenter [NSNotificationCenter defaultCenter]
/** Appdelegate */
#define APPDELEGATE ((AppDelegate *)[[UIApplication sharedApplication] delegate])
/** 获得主Window */
#define KWindow [[UIApplication sharedApplication].delegate window]
/** 弱引用 */
#define WeakSelf(type)  __weak typeof(type) weak##type = type;
/** 强引用 */
#define StrongSelf(type)  __strong typeof(type) strong##type = type;
/** 随机颜色 */
#define KRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
/** 获取系统版本 */
#define iOS_VERSION ([[[UIDevice currentDevice] systemVersion] floatValue])
/** top安全高度 */
#define SafeAreaTopHeight (SCREEN_HEIGHT == 812.0 ? 88 : 64)
/** top安全无导航栏高度 */
#define SafeAreaNoNavTopHeight (SCREEN_HEIGHT == 812.0 ? 44 : 20)
/** bottom安全高度 */
#define SafeAreaBottomHeight (SCREEN_HEIGHT == 812.0 ? 34 : 0)
/** tabbar的高度 */
#define TabbarHeight (SCREEN_HEIGHT == 812.0 ? 83.0 : 49)

#import "HomeViewController.h"
#import "JSInterface.h"
#import "EasyJSWebView.h"
//#import "TabBarViewController.h"
//#import "Masonry.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "UIView+Frame.h"
@interface HomeViewController ()<UIWebViewDelegate>

@property (strong, nonatomic) EasyJSWebView *webView;
@property (strong, nonatomic)  UIActivityIndicatorView *indicatorView;
@property (nonatomic, assign) NSInteger count;


@end

@implementation HomeViewController

//修改useragent
- (void)registUserAgent{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        UIWebView * tempWebView = [[UIWebView alloc] init];
        NSString * oldAgent = [tempWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
        NSString * newAgent = [NSString stringWithFormat:@"iris_1.0 %@",oldAgent];

        NSDictionary * dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
        [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

    });
}


#pragma mark -- 设置状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)setStatusBarBackgroundColor:(UIColor *)color {

    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    self.count ++;
    EasyJSWebView *tempWebView = [EasyJSWebView new];
    [self.view addSubview:tempWebView];
    self.webView = tempWebView;
    self.webView.backgroundColor = [UIColor redColor];
    self.webView.frame = self.view.bounds;
    self.webView.height = self.view.bounds.size.height - STATUS_BAR_HEIGHT;

    //第三方
    JSInterface* interface = [JSInterface new];
    [self.webView addJavascriptInterfaces:interface WithName:@"wst"];
    self.webView.delegate = self;
    self.webView.scrollView.bounces = NO;
    [self.view insertSubview:self.webView aboveSubview:self.navigationController.tabBarController.tabBar];
    [self registUserAgent];

    NSString *url = [NSString stringWithFormat:@"%@liveapp/homepage",@"http://121.18.229.58:20001/"];

    //http://sit.iris.com/
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [_webView loadRequest:request];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hideBottom:) name:@"HIDEBOTTOM" object:nil];

    //添加缓冲显示
    self.indicatorView = [UIActivityIndicatorView new];
    [self.view addSubview:self.indicatorView];
    self.indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    self.indicatorView.hidesWhenStopped = YES;

}

#pragma mark --uiwebview代理方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"开始加载");
    [self startRolling];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"加载完成");
    [self endRolling];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"加载失败");
}

#pragma mark --实现通知方法

- (void)hideBottom:(NSNotification *)sender{
    if ([sender.object isEqualToString:@"1"]) {//显示

      [[NSNotificationCenter defaultCenter]postNotificationName:@"TABBAR_SHOW" object:nil];


    }else if ([sender.object isEqualToString:@"2"]){//隐藏

      [[NSNotificationCenter defaultCenter]postNotificationName:@"TABBAR_HIDDEN" object:nil];
    }
}

- (void)dealloc{
   [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.webView removeFromSuperview];
}

#pragma mark -- 小菊花
- (void)startRolling
{
    [_indicatorView startAnimating];
}
- (void)endRolling
{
    [_indicatorView stopAnimating];
}

@end
